package com.huawei.fastengine.fastview.startFastappEngine.storage;

import android.content.Context;
import com.huawei.fastengine.fastview.download.utils.StringUtils;
import com.huawei.fastengine.fastview.download.utils.log.FastViewLogUtils;
import com.huawei.fastengine.fastview.util.IOFileUtils;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class StorageManager {
    private static final String TAG = "StorageManager";

    private static void clearCacheStorage(String str, Context context) {
        String cacheStoragePath = StorageUtils.getCacheStoragePath(context, str);
        if (IOFileUtils.isExist(cacheStoragePath)) {
            try {
                IOFileUtils.deleteDirectory(new File(cacheStoragePath));
                return;
            } catch (IOException e) {
                FastViewLogUtils.e(TAG, "clearCacheStorage | e:" + e);
                return;
            }
        }
        FastViewLogUtils.e(TAG, "clearCacheStorage| file do not exist.");
    }

    private static void clearDatabaseStorage(String str, Context context) {
        String databasePath = StorageUtils.getDatabasePath(context, str);
        if (StringUtils.isEmpty(databasePath)) {
            FastViewLogUtils.e(TAG, "clearDatabaseStorage | dbFilePath is empty.");
        } else if (IOFileUtils.isExist(databasePath)) {
            try {
                IOFileUtils.forceDelete(new File(databasePath));
            } catch (IOException e) {
                FastViewLogUtils.e(TAG, "clearDatabaseStorage | e:" + e);
            }
        } else {
            FastViewLogUtils.e(TAG, "clearDatabaseStorage| file do not exist.");
        }
    }

    private static void clearFileStorage(String str, Context context) {
        String fileStoragePath = StorageUtils.getFileStoragePath(context, str);
        if (IOFileUtils.isExist(fileStoragePath)) {
            try {
                IOFileUtils.deleteDirectory(new File(fileStoragePath));
                return;
            } catch (IOException e) {
                FastViewLogUtils.e(TAG, "clearFileStorage | e:" + e);
                return;
            }
        }
        FastViewLogUtils.e(TAG, "clearFileStorage| file do not exist.");
    }

    private static void clearMassStorage(String str, Context context) {
        String externalMassPath = StorageUtils.getExternalMassPath(context, str);
        if (StringUtils.isEmpty(externalMassPath)) {
            FastViewLogUtils.e(TAG, "clearMassStorage | massStorage is empty.");
        } else if (IOFileUtils.isExist(externalMassPath)) {
            try {
                IOFileUtils.deleteDirectory(new File(externalMassPath));
            } catch (IOException e) {
                FastViewLogUtils.e(TAG, "clearMassStorage | e:" + e);
            }
        } else {
            FastViewLogUtils.e(TAG, "clearMassStorage| file do not exist.");
        }
    }

    public static void clearStorage(Context context, String str, List<StorageType> list) {
        if (context == null) {
            FastViewLogUtils.e(TAG, "clearStorage | context is null.");
        } else if (StringUtils.isEmpty(str) || list == null || list.size() == 0) {
            FastViewLogUtils.e(TAG, "clearStorage | rpkName is null or storageTypeList is invalid.");
        } else if (!StringUtils.isEmpty(context.getPackageName())) {
            if (list.contains(StorageType.STORAGE_MASS)) {
                clearMassStorage(str, context);
            }
            if (list.contains(StorageType.STORAGE_CACHE)) {
                clearCacheStorage(str, context);
            }
            if (list.contains(StorageType.STORAGE_DATABASE)) {
                clearDatabaseStorage(str, context);
            }
            if (list.contains(StorageType.STORAGE_FILE)) {
                clearFileStorage(str, context);
            }
        }
    }
}
